const watcher = {
	/**
	 * 设置监听器 watch.js
	 */
	setWatcher(page, obj) {
	  try{
		// 获取QQ对象属性数据
		let data = obj;
		// 页面watch 函数
		let watch = page.watch;
		if(!watch) return
		Object.keys(watch).forEach(v => {
			console.log(v)
			// 要监听的属性key
			let key = v.indexOf('.') < 0? v : v.split('.')[1];
			// 假设key==='my.name',此时nowData===data['my']===data.my,lastKey==='name'
			let watchFun = watch[v].handler || watch[v]; // 兼容带handler和不带handler的两种写法
			let deep = watch[v].deep; // 若未设置deep,则为undefine
			observe(data, key, watchFun, deep, page); // 监听nowData对象的lastKey
		})
	  }catch(e){
		  console.log('监听属性变化事件异常:', e)
	  }
	}
}


/**
 * 监听属性 并执行监听函数
 */
function observe(obj, key, watchFun, deep, page) {
  var val = obj[key];
  let that = this;
  Object.defineProperty(obj, key, {
    configurable: true,
    enumerable: false,
    set: function (value) {
      watchFun.call(page, value, val); // value是新值，val是旧值
	  val = value
    },
    get: function () {
      return val;
    }
  })
}

export default watcher